package com.navi.loaddata.core.util;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.navi.loaddata.loadService.impl.measureData.LoadMeasureDataService15;
import org.apache.commons.dbutils.QueryRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.beans.PropertyVetoException;

@Component
public class JdbcUtil {
    private static final Logger logger = LoggerFactory.getLogger(JdbcUtil.class);
    private LoggerUtil loggerUtil = new LoggerUtil(this.getClass());

    static ComboPooledDataSource dataSource_mysql;
    static ComboPooledDataSource dataSource_gp;
    static ComboPooledDataSource dataSource_ora_edaldr;
    static ComboPooledDataSource dataSource_mes;
    static ComboPooledDataSource dataSource_wms;
    static ComboPooledDataSource dataSource_mes_user;
    static ComboPooledDataSource dataSource_ora_spc;

    //mysql
    @Value("${spring.datasource.primary.url}")
    private String primaryUrl;
    @Value("${spring.datasource.primary.username}")
    private String primaryUser;
    @Value("${spring.datasource.primary.password}")
    private String primaryPassword;

    //gp
    @Value("${spring.datasource.secondary.url}")
    private String secondaryUrl;
    @Value("${spring.datasource.secondary.username}")
    private String secondaryUser;
    @Value("${spring.datasource.secondary.password}")
    private String secondaryPassword;

    //太引 ora
    @Value("${spring.datasource.thirdary.url}")
    private String thirdaryUrl;
    @Value("${spring.datasource.thirdary.username}")
    private String thirdaryUser;
    @Value("${spring.datasource.thirdary.password}")
    private String thirdaryPassword;

    //mes ora
    @Value("${spring.datasource.mes.url}")
    private String mesUrl;
    @Value("${spring.datasource.mes.username}")
    private String mesUser;
    @Value("${spring.datasource.mes.password}")
    private String mesPassword;

    //wms ora
    @Value("${spring.datasource.wms.url}")
    private String wmsUrl;
    @Value("${spring.datasource.wms.username}")
    private String wmsUser;
    @Value("${spring.datasource.wms.password}")
    private String wmsPassword;

    @PostConstruct
    public void init() throws PropertyVetoException {
        setMysqlDataSource();
        logger.info("Mysql ch_qms 数据库加载成功！");
        setGpDataSource();
        logger.info("GP qmsprd 数据库加载成功！");
        setOraEdaldrDataSource();
        logger.info("Oracle edaldr 数据库加载成功！");
        setOraMesDataSource();
        logger.info("Oracle MES 数据库加载成功！");
        setOraWmsDataSource();
        logger.info("Oracle WMS 数据库加载成功！");
        setOraMesUserDataSource();
        logger.info("Oracle MES USER 数据库加载成功！");
        setOraSpcDataSource();
        logger.info("Oracle SPC 数据库加载成功！");
    }
    public void setMysqlDataSource(){
        try {
            dataSource_mysql = new ComboPooledDataSource();
            dataSource_mysql.setInitialPoolSize(3);
            dataSource_mysql.setMinPoolSize(2);
            dataSource_mysql.setMaxPoolSize(5);

            dataSource_mysql.setUser(primaryUser);
            dataSource_mysql.setPassword(primaryPassword);
            dataSource_mysql.setDriverClass("com.mysql.jdbc.Driver");
            dataSource_mysql.setJdbcUrl(primaryUrl);

        } catch (PropertyVetoException e) {
            LoggerUtil.stackTraceToString(e);
        }
    }

    public void setGpDataSource(){
        try {
            dataSource_gp = new ComboPooledDataSource();
            dataSource_gp.setInitialPoolSize(0);
            dataSource_gp.setMinPoolSize(0);
            dataSource_gp.setMaxPoolSize(1);

            dataSource_gp.setUser(secondaryUser);
            dataSource_gp.setPassword(secondaryPassword);
            dataSource_gp.setDriverClass("org.postgresql.Driver");
            dataSource_gp.setJdbcUrl(secondaryUrl);
        } catch (PropertyVetoException e) {
            LoggerUtil.stackTraceToString(e);
        }
    }

    public void setOraEdaldrDataSource() {
        try {
            //设置连接池大小
            dataSource_ora_edaldr = new ComboPooledDataSource();
            dataSource_ora_edaldr.setInitialPoolSize(20);
            dataSource_ora_edaldr.setMinPoolSize(10);
            dataSource_ora_edaldr.setMaxPoolSize(50);

            dataSource_ora_edaldr.setUser(thirdaryUser);
            dataSource_ora_edaldr.setPassword(thirdaryPassword);
            dataSource_ora_edaldr.setDriverClass("oracle.jdbc.driver.OracleDriver");
            dataSource_ora_edaldr.setJdbcUrl(thirdaryUrl);

        } catch (PropertyVetoException e) {
            LoggerUtil.stackTraceToString(e);
        }
    }

    public void setOraMesDataSource() {
        try {
            //设置连接池大小
            dataSource_mes = new ComboPooledDataSource();
            dataSource_mes.setInitialPoolSize(20);
            dataSource_mes.setMinPoolSize(10);
            dataSource_mes.setMaxPoolSize(50);

            dataSource_mes.setUser(mesUser);
            dataSource_mes.setPassword(mesPassword);
            dataSource_mes.setDriverClass("oracle.jdbc.driver.OracleDriver");
            dataSource_mes.setJdbcUrl(mesUrl);

        } catch (PropertyVetoException e) {
            LoggerUtil.stackTraceToString(e);
        }
    }

    public void setOraWmsDataSource(){
        try {
            dataSource_wms = new ComboPooledDataSource();
            dataSource_wms.setInitialPoolSize(20);
            dataSource_wms.setMinPoolSize(10);
            dataSource_wms.setMaxPoolSize(50);

            dataSource_wms.setUser(wmsUser);
            dataSource_wms.setPassword(wmsPassword);
            dataSource_wms.setDriverClass("oracle.jdbc.driver.OracleDriver");
            dataSource_wms.setJdbcUrl(wmsUrl);

        } catch (PropertyVetoException e) {
            LoggerUtil.stackTraceToString(e);
        }
    }

    public void setOraMesUserDataSource(){
        try {
            //设置连接池大小
            dataSource_mes_user = new ComboPooledDataSource();
            dataSource_mes_user.setInitialPoolSize(3);
            dataSource_mes_user.setMinPoolSize(2);
            dataSource_mes_user.setMaxPoolSize(5);

            dataSource_mes_user.setUser("P1MESUSER");
            dataSource_mes_user.setPassword("mesuser");
            dataSource_mes_user.setDriverClass("oracle.jdbc.driver.OracleDriver");
            dataSource_mes_user.setJdbcUrl("jdbc:oracle:thin:@//10.50.10.5:1521/P1MESDB");
            dataSource_mes_user.setMaxIdleTime(900);

        } catch (PropertyVetoException e) {
            LoggerUtil.stackTraceToString(e);
        }
    }

    public void setOraSpcDataSource(){
        try {
            //设置连接池大小
            dataSource_ora_spc = new ComboPooledDataSource();
            dataSource_ora_spc.setInitialPoolSize(3);
            dataSource_ora_spc.setMinPoolSize(2);
            dataSource_ora_spc.setMaxPoolSize(5);
            dataSource_ora_spc.setUser("p1spcadm");
            dataSource_ora_spc.setPassword("adm1200");
            dataSource_ora_spc.setDriverClass("oracle.jdbc.driver.OracleDriver");
            dataSource_ora_spc.setJdbcUrl("jdbc:oracle:thin:@//10.50.10.10:1521/p1spcdb");

        } catch (PropertyVetoException e) {
            LoggerUtil.stackTraceToString(e);
        }
    }


    public static QueryRunner getQueryRunner_mes() {
        QueryRunner queryRunner = new QueryRunner(dataSource_mes);
        return queryRunner;
    }

    public static QueryRunner getQueryRunner_wms() {
        QueryRunner queryRunner = new QueryRunner(dataSource_wms);
        return queryRunner;
    }

    public static QueryRunner getQueryRunner_gp() {
        QueryRunner queryRunner = new QueryRunner(dataSource_gp);
        return queryRunner;
    }

    public static QueryRunner getQueryRunner_mes_user() {
        QueryRunner queryRunner = new QueryRunner(dataSource_mes_user);
        return queryRunner;
    }

    public static QueryRunner getQueryRunner_mysql() {
        QueryRunner queryRunner = new QueryRunner(dataSource_mysql);
        return queryRunner;
    }

    public static QueryRunner getQueryRunner_ora_edaldr() {
        QueryRunner queryRunner = new QueryRunner(dataSource_ora_edaldr);
        return queryRunner;
    }

    public static QueryRunner getQueryRunner_ora_spc() {
        QueryRunner queryRunner = new QueryRunner(dataSource_ora_spc);
        return queryRunner;
    }

}
